package com.right.health.app.ui.me;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.MyServiceBean;
import com.right.health.app.bean.ServiceItemBean;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.me.adapter.MyServiceAdapter;
import com.right.health.app.ui.me.adapter.ServiceItemAdapter;
import com.right.health.app.ui.me.adapter.ShippingAddressAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @author: weming
 * @date: 2021/5/7
 * @description:我的服务包
 */
public class MyServicePackActivity extends BaseActivity implements OnHttpListener, View.OnClickListener {

    private final int HTTP_CODE_GET_LIST = 1;

    private ListView mListView;
    private MyServiceAdapter mAdapter;
    private UserApi mUserApi;
    private ProgressDialog mProgressDialog;
    private boolean mFirstResume=true;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_my_service_pack);
    }

    @Override
    public void findView() {
        View emptyView = findViewById(R.id.empty_view);
        mListView = findViewById(R.id.listView);
        mListView.setEmptyView(emptyView);
        mAdapter = new MyServiceAdapter(this);
        mListView.setAdapter(mAdapter);
        mAdapter.setClickListener(this);
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_complete) {
            MyServiceBean bean = (MyServiceBean) v.getTag();
            if (bean.getOrderstatus().equals("0")) {
                ServiceOrderActivity.launch(this,bean.getOrderid(),bean.getOrdercode(),bean.getPkname(),bean.getHospital(),bean.getPkprice(),1000);
            }else {
                  MyServiceDetailActivity.launch(this,bean.getOrderid());
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_GET_LIST) {
                try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    List<MyServiceBean> list = new Gson().fromJson(jsonObject.optString("orders"), new TypeToken<List<MyServiceBean>>() {
                    }.getType());
                    mAdapter.setDataSource(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(this, "数据解析失败");
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mFirstResume) {
            mFirstResume=false;
            mProgressDialog = ProgressDialog.show(this, "", "加载中",false,true);
        }
        mUserApi.addRequestCode(HTTP_CODE_GET_LIST);
        mUserApi.getMyServicePack();
    }
}
